package com.gk.panda.apis.controller;

import com.alibaba.nacos.common.utils.HttpMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.dto.BoundaryDto;
import com.gk.panda.apis.dto.BoundarySaveDto;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.BoundaryService;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.Boundary;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 勘界信息控制器
 */
@RestController
@RequestMapping("/boundary")
@Api(tags = "勘界信息")
public class BoundaryController {

    @Autowired
    private BoundaryService boundaryService;


    @ApiOperation(value = "添加勘界信息")
    @Log
    @PostMapping
    public Result<String> addBoundary(@RequestBody BoundarySaveDto dto) {
        boolean save = boundaryService.addBoundary(dto);
        if (save) return Result.write(200,"添加成功");
        return Result.write(201,"添加失败");
    }

    @ApiOperation(value = "分页查询勘界信息")
    @PostMapping("/page")
    public Result<Page<Boundary>> getBoundary(@RequestBody BoundaryDto dto) {
        Page<Boundary> boundaryPage= boundaryService.getBoundary(dto);
        return Result.ok(boundaryPage);

    }

    @ApiOperation(value = "修改勘界信息")
    @Log
    @PutMapping
    public Result<String> updateBoundary(@RequestBody Boundary boundary) {
        boolean update = boundaryService.updateById(boundary);
        if (update) return Result.write(200,"修改成功");
        return Result.write(201,"修改失败");
    }

    @ApiOperation(value = "删除勘界信息")
    @Log
    @DeleteMapping("/{id}")
    public Result<String> deleteBoundary(@PathVariable("id") int id) {
        boolean remove = boundaryService.removeById(id);
        if (remove) return Result.write(200,"删除成功");
        return Result.write(201,"删除失败");
    }

    @ApiOperation(value = "下载勘界导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        boundaryService.downloadTemplate(response);
    }

    @ApiOperation(value = "批量导入勘界信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/import")
    public Result<String> importBoundary(@RequestPart("file") MultipartFile file) {
        String result =  boundaryService.importBoundary(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导出勘界信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/export")
    public void exportBoundary(HttpServletResponse response) {
        boundaryService.exportBoundary(response);
    }

}
